package com.yzjs.localhost.service.tNeusoftBigDataTesting.impl;

import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.common.util.httpclient.HttpUtils;
import com.yzjs.localhost.dictionaries.DealDictionary;
import com.yzjs.localhost.dto.TNeusoftBigDataTestingEntityDTO;
import com.yzjs.localhost.entity.tNeusoftBigDataTesting.TNeusoftBigDataTestingEntity;
import com.yzjs.localhost.dao.tNeusoftBigDataTesting.TNeusoftBigDataTestingMapper;
import com.yzjs.localhost.service.tNeusoftBigDataTesting.TNeusoftBigDataTestingService;
import com.yzjs.localhost.service.tNeusoftComplianceTesting.impl.TNeusoftComplianceTestingServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.apache.logging.log4j.Logger;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("tNeusoftBigDataTestingService")
public class TNeusoftBigDataTestingServiceImpl extends BaseServiceImpl<TNeusoftBigDataTestingEntity, TNeusoftBigDataTestingMapper> implements TNeusoftBigDataTestingService {

    @Value("${bdms.url}")
    private String  bdmsUrl ;

    @Value("${doong.push.url}")
    private String doongPushUrl ;

    private static Logger logger = LogManager.getLogger(TNeusoftComplianceTestingServiceImpl.class.getName());

    @Override
    public void deal(TNeusoftBigDataTestingEntity tNeusoftBigDataTestingEntity) throws Exception{
        if(DealDictionary.PROCESSED ==tNeusoftBigDataTestingEntity.getDisposalStatus() ){
            TNeusoftBigDataTestingEntity query = new TNeusoftBigDataTestingEntity();
            query.setId(tNeusoftBigDataTestingEntity.getId());
            TNeusoftBigDataTestingEntity result = this.findOne(query);

            String restJson = "operationId="+result.getOperationId()+",jurisdictionCode="+result.getJurisdictionCode()+
                    ",warningInfo="+result.getWarningInfo();
            String url = doongPushUrl+"/auth/callback/notify?appSecret=n9wHjD9lJwdEP3YFnHTH7C814f6E4YgTDhG41iUZJsKfWVYCTxnMyyhE05d&restJson="+ URLEncoder.encode(restJson, "UTF-8");

            logger.info(url);
            // 调用东软服务接口
            String resultOut  = HttpUtils.post(url,new HashMap<>(),5000);
            logger.info("---------------------NeusoftBigDataTesting  返回结果信息 "+resultOut);
        }

        this.update(tNeusoftBigDataTestingEntity);
    }

    /**
     * 合规性获取详情
     * @param query
     * @return
     */
    @Override
    public TNeusoftBigDataTestingEntityDTO getDetail(TNeusoftBigDataTestingEntity query) throws Exception {
        TNeusoftBigDataTestingEntityDTO  dto = new TNeusoftBigDataTestingEntityDTO();
        TNeusoftBigDataTestingEntity tNeusoftBigDataTestingEntity  = this.findOne(query);
        dto.setTNeusoftBigDataTestingEntity(tNeusoftBigDataTestingEntity);

        //获取区块链链码信息
        String dbUrl = bdmsUrl+"/v1/datasourcesettings/list?table_name=t_neusoft_big_data_testing";
        String result= HttpUtils.get(dbUrl,5000);
        Map<String,Object> tableMap = JsonUtil.json2map(result);
        List<Map<String,Object>> list = (List<Map<String, Object>>) tableMap.get("data");
        if(list != null &&list.size()>0){
            Integer dsId = (Integer) list.get(0).get("id");
            String tableName = (String) list.get(0).get("table_name");
            String onchainDataUrl = bdmsUrl+"/v1/basis_highlevel/onchain_data/?key=doonsoft.t_neusoft_big_data_testing.qwertyuiopasdfghjklzxcvbnm"+"&ds_id="+dsId;
            //String onchainDataUrl= "http://10.10.1.45/hbxy/v1/basis_highlevel/onchain_data/?key=BDMS.t_dictionary_field.1&ds_id=9133";
            String resultData = HttpUtils.get(onchainDataUrl,5000);
            dto.setOnChainData(resultData);
        }
        return dto;
    }
}